from typing import Optional
from pydantic import BaseModel, Field, validator
import re


class TwitterUrlRequest(BaseModel):
    """Twitter URL请求模型"""
    url: str = Field(..., description="Twitter/X视频链接")
    
    @validator('url')
    def validate_twitter_url(cls, v):
        """验证Twitter URL格式"""
        if not v:
            raise ValueError("URL不能为空")
        
        # Twitter URL正则模式
        twitter_patterns = [
            r'https?://(www\.)?(twitter\.com|x\.com|mobile\.twitter\.com)/.+/status/\d+',
            r'https?://(www\.)?(twitter\.com|x\.com)/i/web/status/\d+',
        ]
        
        is_valid = any(re.match(pattern, v) for pattern in twitter_patterns)
        
        if not is_valid:
            raise ValueError(
                "无效的Twitter链接格式。支持的格式：\n"
                "- https://twitter.com/username/status/TWEET_ID\n"
                "- https://x.com/username/status/TWEET_ID\n"
                "- https://mobile.twitter.com/username/status/TWEET_ID\n"
                "- https://twitter.com/i/web/status/TWEET_ID"
            )
        
        return v


class TwitterQualityRequest(BaseModel):
    """Twitter质量下载请求模型"""
    url: str = Field(..., description="Twitter/X视频链接")
    quality: Optional[str] = Field("best", description="视频质量")
    
    @validator('url')
    def validate_twitter_url(cls, v):
        """验证Twitter URL格式"""
        if not v:
            raise ValueError("URL不能为空")
        
        # Twitter URL正则模式
        twitter_patterns = [
            r'https?://(www\.)?(twitter\.com|x\.com|mobile\.twitter\.com)/.+/status/\d+',
            r'https?://(www\.)?(twitter\.com|x\.com)/i/web/status/\d+',
        ]
        
        is_valid = any(re.match(pattern, v) for pattern in twitter_patterns)
        
        if not is_valid:
            raise ValueError(
                "无效的Twitter链接格式。支持的格式：\n"
                "- https://twitter.com/username/status/TWEET_ID\n"
                "- https://x.com/username/status/TWEET_ID\n"
                "- https://mobile.twitter.com/username/status/TWEET_ID\n"
                "- https://twitter.com/i/web/status/TWEET_ID"
            )
        
        return v
    
    @validator('quality')
    def validate_quality(cls, v):
        """验证质量参数"""
        if v is None:
            return "best"
        
        valid_qualities = ["1080p", "720p", "480p", "360p", "240p", "best", "worst"]
        if v not in valid_qualities:
            raise ValueError(f"不支持的质量选项: {v}. 支持: {', '.join(valid_qualities)}")
        return v


class TwitterUsernameRequest(BaseModel):
    """Twitter用户名请求模型"""
    username: str = Field(..., description="Twitter用户名")
    count: Optional[int] = Field(20, description="获取推文数量，默认20条")
    
    @validator('username')
    def validate_username(cls, v):
        """验证用户名格式"""
        if not v:
            raise ValueError("用户名不能为空")
        
        # 移除可能的@符号
        v = v.lstrip('@')
        
        # 验证用户名格式 (字母、数字、下划线，1-15个字符)
        if not re.match(r'^[a-zA-Z0-9_]{1,15}$', v):
            raise ValueError("无效的Twitter用户名格式")
        
        return v
    
    @validator('count')
    def validate_count(cls, v):
        """验证数量参数"""
        if v is None:
            return 20
        
        if v < 1 or v > 100:
            raise ValueError("获取数量应在1-100之间")
        
        return v 